#include "file_dlg.h"

//==============================================================================
// Global file dialogs
//==============================================================================
FOPEN_DIALOG fopenDlg;
FOPEN_DIALOG fsaveDlg;

//==============================================================================
// Constructor
//==============================================================================
FOPEN_DIALOG::FOPEN_DIALOG () {
	Visible = false;
}

//==============================================================================
// Creates an instance of the file dialog
//==============================================================================
void FOPEN_DIALOG::Create (const G3::RECTI &rect, G3::FONT *font, const std::string &title, bool for_saving) {
    G3::RECTI fpr (rect.Left + 10, rect.Top + 40, rect.Right - 10, rect.Top + 60);
    G3::RECTI br (rect.Right - 65, rect.Bottom - 35, rect.Right - 13, rect.Bottom - 10);
    G3::RECTI br2 (rect.Left + 10, rect.Bottom - 35, rect.Left + 80, rect.Bottom - 10);

    group.Create (rect, font, title);
    text.Create (G3::VEC2I (rect.Left + 10, rect.Top + 20), font);
    filepath.Create (fpr, font);

    text.SetValue ("Type the path here:");

    if (for_saving) {
        ok_button.Create (br, font, "Save");

        filepath.doOnEnterPressed (SaveTheSpline);
        ok_button.doOnMouseClicked (SaveTheSpline);
    } else {
        ok_button.Create (br, font, "Open");

        filepath.doOnEnterPressed (LoadTheSpline);
        ok_button.doOnMouseClicked (LoadTheSpline);
	}

	cancel_button.Create (br2, font, "Cancel");
	cancel_button.doOnMouseClicked (CloseFileWindow);

    group.AttachWidget (&text);
    group.AttachWidget (&filepath);
    group.AttachWidget (&ok_button);
	group.AttachWidget (&cancel_button);

    G3::GUI::MainGui.AddWidget (NULL, &group);
    G3::GUI::MainGui.AddWidget (NULL, &text);
    G3::GUI::MainGui.AddWidget (NULL, &filepath);
    G3::GUI::MainGui.AddWidget (NULL, &ok_button);
	G3::GUI::MainGui.AddWidget (NULL, &cancel_button);

    Hide ();
}

//==============================================================================
// Hides the dialog
//==============================================================================
void FOPEN_DIALOG::Hide () {
    group.Hide ();
    group.Disable ();
    text.Hide ();
    text.Disable ();
    filepath.Hide ();
    filepath.Disable ();
    ok_button.Hide ();
    ok_button.Disable ();
	cancel_button.Hide ();
	cancel_button.Disable ();

    Visible = false;
}

//==============================================================================
// Shows the dialog
//==============================================================================
void FOPEN_DIALOG::Show () {
    group.Show ();
    group.Enable ();
    text.Show ();
    text.Enable ();
    filepath.Show ();
    filepath.Enable ();
    ok_button.Show ();
    ok_button.Enable ();
	cancel_button.Show ();
	cancel_button.Enable ();
    G3::GUI::MainGui.SetFocus (&filepath);

	filepath.SetValue ("");

    Visible = true;
}

//==============================================================================
// Save button clickhandler
//==============================================================================
void SaveTheSpline (G3::GUI::EDITBOX *aEBox) {
    Assert (aEBox, "SaveTheSpline: editbox argument is a NULL pointer..\n");

    Spline.Save (aEBox->GetValue ());

    fsaveDlg.Hide ();
}

void SaveTheSpline (G3::INPUT *aInput, G3::GUI::WIDGET *aWidget) {
    Spline.Save (fsaveDlg.filepath.GetValue ());

    fsaveDlg.Hide ();
}

//==============================================================================
// Load button clickhandler
//==============================================================================
void LoadTheSpline (G3::GUI::EDITBOX *aEBox) {
    Assert (aEBox, "LoadTheSpline: editbox argument is a NULL pointer..\n");

    Spline.Load (aEBox->GetValue ());

    fopenDlg.Hide ();
}

void LoadTheSpline (G3::INPUT *aInput, G3::GUI::WIDGET *aWidget) {
    Spline.Load (fopenDlg.filepath.GetValue ());

    fopenDlg.Hide ();
}

//==============================================================================
// Cancel button clickhandler
//==============================================================================
void CloseFileWindow (G3::INPUT *aInput, G3::GUI::WIDGET *aWidget) {
	fopenDlg.Hide ();
	fsaveDlg.Hide ();
}

